<template>
<div class="course">
  <el-form :inline="true">
    <el-form-item>
      <el-select v-model="schoolTerm" placeholder="Please select a semester">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-input v-model="subject" placeholder="Please enter a major"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="success" @click="getCourseList">Search Course</el-button>
      <el-button type="primary" @click="addCourse">Add Course</el-button>
    </el-form-item>
  </el-form>
  <el-table
    :data="tableData"
    width="100%"
    :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
    border>
    <el-table-column
      align="center"
      prop="classSection"
      label="Period"
      min-width="200"
      >
      <template slot-scope="scope">
        <div class="box-2">
          {{scope.row.classSection}}
        </div>
      </template>
    </el-table-column>
    <el-table-column label="Monday" min-width="200" align="center">
      <template slot-scope="scope">
        <div class="box-1">
          <div v-if="scope.row.oneWeek != null">
            <el-tag closable @close="handleClose(scope.row.oneWeek.id)">{{scope.row.oneWeek.courseName}}</el-tag><br>
          Teacher：{{scope.row.oneWeek.teacherName}}<br>
          Address：{{scope.row.oneWeek.courseAddress}}
          </div>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="Tuesday" min-width="200" align="center">
      <template slot-scope="scope" v-if="scope != null">
        <div v-if="scope.row.twoWeek != null">
          <el-tag closable @close="handleClose(scope.row.twoWeek.id)">{{scope.row.twoWeek.courseName}}</el-tag><br>
        Teacher：{{scope.row.twoWeek.teacherName}}<br>
        Address：{{scope.row.twoWeek.courseAddress}}
        </div>
      </template>
    </el-table-column>
    <el-table-column label="Wednesday" min-width="200" align="center">
      <template slot-scope="scope" v-if="scope != null">
        <div v-if="scope.row.threeWeek != null">
          <el-tag closable @close="handleClose(scope.row.threeWeek.id)">{{scope.row.threeWeek.courseName}}</el-tag><br>
        Teacher：{{scope.row.threeWeek.teacherName}}<br>
        Address：{{scope.row.threeWeek.courseAddress}}
        </div>
      </template>
    </el-table-column>
    <el-table-column label="Thursday" min-width="200" align="center">
      <template slot-scope="scope" v-if="scope != null">
        <div v-if="scope.row.fourWeek != null">
          <el-tag closable @close="handleClose(scope.row.fourWeek.id)">{{scope.row.fourWeek.courseName}}</el-tag><br>
        Teacher：{{scope.row.fourWeek.teacherName}}<br>
        Address：{{scope.row.fourWeek.courseAddress}}
        </div>
      </template>
    </el-table-column>
    <el-table-column label="Friday" min-width="200" align="center">
      <template slot-scope="scope" v-if="scope != null">
        <div v-if="scope.row.fiveWeek != null">
          <el-tag closable @close="handleClose(scope.row.fiveWeek.id)">{{scope.row.fiveWeek.courseName}}</el-tag><br>
        Teacher：{{scope.row.fiveWeek.teacherName}}<br>
        Address：{{scope.row.fiveWeek.courseAddress}}
        </div>
      </template>
    </el-table-column>
  </el-table>
  <courseDialog v-if="addVisible" ref="courseDialog" @refreshDataList="getCourseList"></courseDialog>
  </div>
</template>
<script>
  import courseDialog from './courseDialog'
  export default {
    data () {
      return {
        tableData: [],
        options: [
          {
            value: 1,
            label: 'First semester'
          },
          {
            value: 2,
            label: 'Second semester'
          },
          {
            value: 3,
            label: 'The third semester'
          },
          {
            value: 4,
            label: 'The fourth semester'
          }
        ],
        schoolTerm: '',
        subject: '',
        addVisible: false
      }
    },
    components: {
      courseDialog
    },
    activated () {
      this.getCourseList()
    },
    methods: {
      // 删除课程
      handleClose (id) {
        console.log(id)
        this.$http({
          url: this.$http.adornUrl('/edu/course/deleteCourse/' + id),
          method: 'delete'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message.success('Course removed successfully!')
            this.getCourseList()
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      // 添加课程
      addCourse () {
        this.addVisible = true
        this.$nextTick(() => {
          this.$refs.courseDialog.init()
        })
      },
      // 获取课程表数据
      getCourseList () {
        this.$http({
          url: this.$http.adornUrl('/edu/course/courseList'),
          method: 'get',
          params: this.$http.adornParams({
            'schoolTerm': this.schoolTerm,
            'subject': this.subject
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.tableData = data.data
          } else {
            this.$message.error(data.msg)
          }
        })
      }
    }
  }
</script>

<style lang="scss">
.course{
  .box-1{
    height: 70px;
  }
  .el-table_1_column_1{
    background:#f5f7fa !important;
    color:#606266 !important ;
  } 
}
</style>
